﻿<?php
include 'test_header.php';

class test_class_code_process extends UnitTestCase{
	
	private $code_file_name_for_test ;

	function setUp() {
		$db = db_connect();
		$sql = "
CREATE TABLE IF NOT EXISTS `code` (
  `code_file_name` varchar(64) NOT NULL,
  `problem_no` int(11) NOT NULL,
  `username` varchar(256) NOT NULL,
  `language` varchar(16) NOT NULL,
  `modify_time` datetime NOT NULL,
  `create_time` datetime NOT NULL,
  `time_limit` varchar(32) not null,
  `mem_limit`  varchar(32) not null ,
  `content` text NOT NULL,
  KEY `username` (`username`(255))
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
" ;

	$res = $db->query( $sql );
	
	$this->code_file_name_for_test = rand().'.c';

	$sql = "
INSERT INTO `code` (`code_file_name`, `problem_no`, `username`, `language`, `modify_time`, `create_time`, `time_limit`,`mem_limit`,`content`) VALUES
('".$this->code_file_name_for_test."', 1000, 'judasnow', 'ANSI C', '2010-05-15 12:04:47', '2010-05-15 12:04:47', '123', '1000', '#include <stdio.h>\r\n\r\nint\r\nmain(){\r\nprintf(''test'');\r\nreturn 0 ;\r\n}')	
";

	$res = $db->query( $sql );

}
	function tearDown(){ 
		
		$db = db_connect();
		$sql = 'DROP TABLE `code`';
	//	$res = $db->query( $sql );

	}

	function test_create_suffix(){

		//for ANSI C
		$db = db_connect();
		$request = array(
			'problem_no' => '1000' ,
			'username' => 'tester' ,
			'language' => 'ANSI C' ,
			'content' => 'testtsetsetseste' ,
			'time_limit' => '123' ,
			'mem_limit' => '123123' 
		);
		$code_process = new code_process( $db , $request );
		$this->assertTrue( $code_process->create_suffix() , '.c' ) ;

		//for cpp
		$request = array(
			'problem_no' => '1000' ,
			'username' => 'tester' ,
			'language' => 'C++' ,
			'content' => 'testtsetsetseste' ,
			'time_limit' => '123' ,
			'mem_limit' => '123123' 
		);
		$code_process = new code_process( $db , $request );
		$this->assertTrue( $code_process->create_suffix() , '.cpp' ) ;

		//未定义的语言
		$request = array(
			'problem_no' => '1000' ,
			'username' => 'tester' ,
			'language' => 'java' ,
			'content' => 'testtsetsetseste',
			'time_limit' => '123' ,
			'mem_limit' => '123123' 
		);
		$code_process = new code_process( $db , $request );
		$this->expectException();
		$code_process->create_suffix() ;

	}


	function test_just_save_user_code(){
		
		$db = db_connect();
		$request = array(
			'problem_no' => '1000' ,
			'username' => 'tester' ,
			'language' => 'ANSI C' ,
			'content' => 'testtsetsetseste',
			'time_limit' => '123' ,
			'mem_limit' => '123123' 
		);
		
		//保存代码需要创建文件名
		//文件更改时间,创建时间
		//等信息,这些都是code_process类的
		//职责
		$code_process = new code_process( $db , $request );
		$this->assertTrue( $code_process->save() );
	}    

	function test_create_a_temp_file_for_judege(){

		//直接创建文件
		$db = db_connect();
		$request = array(
			'problem_no' => '1000' ,
			'username' => 'tester' ,
			'language' => 'ANSI C' ,
			'content' => 'testtsetsetseste',
			'time_limit' => '123' ,
			'mem_limit' => '123123' 
		);
		
		$code_process = new code_process( $db , $request );
		$this->assertTrue( $code_process->save() );
		$this->assertTrue( $code_process->create_temp_file_to_judge() );
	//	$this->assertTrue( $code_process->delete_temp_file() ) ;
		
	}

	//用户之前就已保存的代码
	function test_judge_code_for_pass_code(){
	
		$db = db_connect();
		$code_process = new code_process( $db );
	
		//从数据库中取出setUp函数中插入的记录
		$this->assertTrue( $code_process->load( 'code_file_name' , $this->code_file_name_for_test ) );

		$this->assertTrue( $code_process->do_judge() ) ;

	}

	function test_fetch_all_code_form_database_by_username(){

		$db = db_connect();
		$rows = code_process::get_all_code_by_user_name( $db , 'tester' );
		$this->assertTrue( $rows );
	}
}
	
$test = new test_class_code_process();
$test->run( new HtmlReporter() );
